package com.mftour.common.utils;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationObjectSupport;

public class WebContentBean extends WebApplicationObjectSupport{

	/** ApplicationContext */
	private static WebApplicationContext cfx = null;
	/** application */
	private static ServletContext application = null;
	/** 站点的物理路径 */
	public static String realPath = null;
	/** 根路径 */
	private final static String userBasePath = "webRoot//";
	
	private final static String classFoldPath = "classes//";
	/** 获取根路径 */
	public String getUserBasePath() {
		return userBasePath;
	}
	//获取classes文件夹路径
	public static String getClassesPath(){
		return realPath + "WEB-INF//" + classFoldPath;
	}

	public static ServletContext getApplication() {
		return application;
	}
	
	public static WebApplicationContext getCfx() {
		return cfx;
	}
	
	/**
	 * 获取beanName
	 * @param beanName
	 * @return
	 */
	public Object getBean(String beanName) {
		if (cfx == null)
			return null;
		return cfx.getBean(beanName);
	}

	/**
	 * 从application缓存中获取参数对象
	 * 
	 * @param name
	 * @return
	 */
	public Object getObjByCache(String name) {
		if (application == null) {
			return null;
		}
		return application.getAttribute(name);
	}
	
	@Override
	protected void initApplicationContext(ApplicationContext context) {
		super.initApplicationContext(context);
		WebContentBean.cfx=(WebApplicationContext)context;		
	}
	
	@Override
	protected void initServletContext(ServletContext servletContext) {
		super.initServletContext(servletContext);
		WebContentBean.application=servletContext;
//		getInstance().setApplication(servletContext);
	}
	
	
}

